<template>
  <a-upload
    :accept="props.accept"
    :showUploadList="false"
    :customRequest="customRequest"
    :beforeUpload="beforeUpload"
    style="display: inline-block"
  >
    <slot>
      <a-button size="small" type="primary">导入</a-button>
    </slot>
  </a-upload>
</template>
<script setup>
import { uploadFile } from 'utils/file.js'
// import { reactive, toRefs, ref } from 'vue'
import { message } from 'ant-design-vue'
const props = defineProps({
  disabled: {
    type: Boolean,
    default: false
  },
  size: {
    type: Number,
    default: 10
  },
  accept: {
    type: String,
    default: ''
  },
  multiple: {
    type: Boolean,
    default: false
  },
  action: {
    type: String,
    default: ''
  },
  params: {
    type: Object,
    default: function () {
      return {}
    }
  }
})
const emits = defineEmits(['succsss'])
const beforeUpload = file => {
  return new Promise((resolve, reject) => {
    const isLt = file.size / 1024 / 1024 < props.size
    if (!isLt) {
      message.error('File must smaller than ' + props.size + 'MB!')
      return reject(false)
    }
    return resolve(true)
  })
}
const customRequest = async data => {
  const params = {
    ...props.params,
    file: [data.file]
  }
  if (props.params.fabricType) {
    params.type = props.params.fabricType
  }
  console.log(params, 'params')
  const result = await uploadFile(props.action, params)
  if (result) {
    emits('success', result)
  }
}
</script>
<style lang="scss" scoped></style>
